home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10610 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  52 lines

  1. Path: dash.com!news
  2. From: Jim Bosshardt <jbossha@dash.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Newbie needs help w/ recursion
  5. Date: 18 Mar 1996 23:04:57 GMT
  6. Organization: Shaman Exchange
  7. Message-ID: <4ikq6p$io1@impsets.dash.com>
  8. NNTP-Posting-Host: 198.199.231.42
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 1.1N (Macintosh; I; 68K)
  13. X-URL: news:comp.lang.c
  14.  
  15. Hi, I am new and am about to pull my hair out over the supposedly simple 
  16. recursion problem I have. The function is to take a number and raise it 
  17. to a neg or pos power and return the value to main().  I need to take 
  18. the following and make it a recursive function...
  19.  
  20. double power (double a, float b)
  21. {
  22.             double power = 1;
  23.             int i;
  24.      
  25.             if (a == 0)
  26.        pow = 0;
  27.             else if (b == 0)
  28.        pow = 1;
  29.    else if Ib > 0)
  30.    {
  31.           for(i = 1; i <= b; i++)
  32.            pow *= a;
  33.    }
  34.    else if (b < 0)
  35.    {
  36.        b = -b;
  37.        for (i = 1; i <= b; i+)
  38.             pow *= a;
  39.        return 1/pow;
  40.     }
  41.     return pow;
  42. }
  43.  
  44. I can only find recursion examples that deal with factorials and cannot 
  45. seem to get the function to be recursive.  I have struggled with this 
  46. for days and would really appreciate any help any body can give me.  
  47. Thanks!
  48.  
  49. Dianna Bosshardt  
  50.  
  51.  
  52.